package com.ly.heart.service.impl;

import java.util.List;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.core.domain.entity.SysDept;
import com.ly.common.core.domain.entity.SysUser;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.*;
import com.ly.heart.service.*;
import com.ly.system.service.ISysDeptService;
import com.ly.system.service.ISysUserService;
import org.springframework.stereotype.Service;
import com.ly.heart.mapper.KingFinanceReceiptSettlementBillMapper;

import javax.annotation.Resource;

/**
 * 金蝶-财务-应收票据结算单Service业务层处理
 *
 * @author wutao
 * @date 2024-06-23
 */
@Service
public class KingFinanceReceiptSettlementBillServiceImpl extends ServiceImpl<KingFinanceReceiptSettlementBillMapper, KingFinanceReceiptSettlementBillEntity> implements IKingFinanceReceiptSettlementBillService {

    @Resource
    private KingFinanceReceiptSettlementBillMapper kingFinanceReceiptSettlementBillEntityMapper;
    @Resource
    private ICustomerService iCustomerService;
    @Resource
    private ISupplierService iSupplierService;
    @Resource
    private ISysDeptService iSysDeptService;
    @Resource
    private ISysUserService iSysUserService;
    @Resource
    private IKingBaseTransactionsCompanyService iKingBaseTransactionsCompanyService;
    @Resource
    private IKingBaseBankService iKingBaseBankService;
    @Resource
    private IKingFinanceReceiptBillReciteService iKingFinanceReceiptBillReciteService;

    /**
     * 查询金蝶-财务-应收票据结算单
     *
     * @param id 金蝶-财务-应收票据结算单ID
     * @return 金蝶-财务-应收票据结算单
     */
    @Override
    public KingFinanceReceiptSettlementBillEntity selectKingFinanceReceiptSettlementBillEntityById ( String id ) {
        KingFinanceReceiptSettlementBillEntity receiptSettlementBillEntity = kingFinanceReceiptSettlementBillEntityMapper.selectKingFinanceReceiptSettlementBillEntityById ( id );
        if ( receiptSettlementBillEntity.getReceiptBillType () == 1 || receiptSettlementBillEntity.getReceiptBillType () == 2 ) {
            String companyName = getCompanyName ( receiptSettlementBillEntity.getEndorsedDeptType (), receiptSettlementBillEntity.getEndorsedDeptId (), 1 );
            receiptSettlementBillEntity.setEndorsedDeptName ( companyName );
        }
        if ( StringUtils.isNotBlank ( receiptSettlementBillEntity.getTransactionCompanyType () ) && StringUtils.isNotBlank ( receiptSettlementBillEntity.getTransactionCompanyId () ) ) {
            String companyName = getCompanyName ( receiptSettlementBillEntity.getTransactionCompanyType (), receiptSettlementBillEntity.getTransactionCompanyId (), 1 );
            receiptSettlementBillEntity.setTransactionCompanyName ( companyName );
        }
        if ( StringUtils.isNotBlank ( receiptSettlementBillEntity.getEndorsedDeptType () ) && StringUtils.isNotBlank ( receiptSettlementBillEntity.getEndorsedDeptId () ) ) {
            String endorse = getCompanyName ( receiptSettlementBillEntity.getEndorsedDeptType (), receiptSettlementBillEntity.getEndorsedDeptId (), 1 );
            receiptSettlementBillEntity.setEndorsedDeptName ( endorse );
        }
        List<KingFinanceReceiptBillReciteEntity> list = iKingFinanceReceiptBillReciteService.list (new LambdaQueryWrapper<KingFinanceReceiptBillReciteEntity> ().
                eq ( KingFinanceReceiptBillReciteEntity::getReceiptBillId, receiptSettlementBillEntity.getReceiptBillId () ) );
        receiptSettlementBillEntity.setKingFinanceReceiptBillReciteEntityList ( list );
        return receiptSettlementBillEntity;
    }

    /**
     * 查询金蝶-财务-应收票据结算单列表
     *
     * @param kingFinanceReceiptSettlementBillEntity 金蝶-财务-应收票据结算单
     * @return 金蝶-财务-应收票据结算单
     */
    @Override
    public List<KingFinanceReceiptSettlementBillEntity> selectKingFinanceReceiptSettlementBillEntityList ( KingFinanceReceiptSettlementBillEntity kingFinanceReceiptSettlementBillEntity ) {
        kingFinanceReceiptSettlementBillEntity.setPage ( ( kingFinanceReceiptSettlementBillEntity.getPage () - 1 ) * kingFinanceReceiptSettlementBillEntity.getLimit () );
        List<KingFinanceReceiptSettlementBillEntity> kingFinanceReceiptSettlementBillEntities = kingFinanceReceiptSettlementBillEntityMapper.selectKingFinanceReceiptSettlementBillEntityList ( kingFinanceReceiptSettlementBillEntity );
        kingFinanceReceiptSettlementBillEntities.forEach ( settlementBill -> {
            if ( StringUtils.isNotBlank ( settlementBill.getTransactionCompanyType () ) && StringUtils.isNotBlank ( settlementBill.getTransactionCompanyId () ) ) {
                String companyName = getCompanyName ( settlementBill.getTransactionCompanyType (), settlementBill.getTransactionCompanyId (), 1 );
                settlementBill.setTransactionCompanyName ( companyName );
            }
            if ( StringUtils.isNotBlank ( settlementBill.getEndorsedDeptType () ) && StringUtils.isNotBlank ( settlementBill.getEndorsedDeptId () ) ) {
                String endorse = getCompanyName ( settlementBill.getEndorsedDeptType (), settlementBill.getEndorsedDeptId (), 1 );
                settlementBill.setEndorsedDeptName ( endorse );
            }
        } );
        return kingFinanceReceiptSettlementBillEntities;
    }

    @Override
    public Integer selectKingFinanceReceiptSettlementBillCount ( KingFinanceReceiptSettlementBillEntity kingFinanceReceiptSettlementBillEntity ) {
        Integer count = kingFinanceReceiptSettlementBillEntityMapper.selectKingFinanceReceiptSettlementBillCount ( kingFinanceReceiptSettlementBillEntity );
        return count;
    }

    /**
     * 新增金蝶-财务-应收票据结算单
     *
     * @param kingFinanceReceiptSettlementBillEntity 金蝶-财务-应收票据结算单
     * @return 结果
     */
    @Override
    public int insertKingFinanceReceiptSettlementBillEntity ( KingFinanceReceiptSettlementBillEntity kingFinanceReceiptSettlementBillEntity ) {
        kingFinanceReceiptSettlementBillEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        int count = this.count ();
        kingFinanceReceiptSettlementBillEntity.setDocNo ( "YSPJJSD".concat ( String.format ( "%08d", count ) ) );
        kingFinanceReceiptSettlementBillEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingFinanceReceiptSettlementBillEntity.setCreateTime ( DateUtils.getNowDate () );
        boolean save = this.save ( kingFinanceReceiptSettlementBillEntity );
        if ( ! save ) {
            throw new CustomException ( "新增应收票据结算单失败！" );
        }
        return 1;
    }

    /**
     * 修改金蝶-财务-应收票据结算单
     *
     * @param kingFinanceReceiptSettlementBillEntity 金蝶-财务-应收票据结算单
     * @return 结果
     */
    @Override
    public int updateKingFinanceReceiptSettlementBillEntity ( KingFinanceReceiptSettlementBillEntity kingFinanceReceiptSettlementBillEntity ) {
        kingFinanceReceiptSettlementBillEntity.setUpdateTime ( DateUtils.getNowDate () );
        return kingFinanceReceiptSettlementBillEntityMapper.updateKingFinanceReceiptSettlementBillEntity ( kingFinanceReceiptSettlementBillEntity );
    }

    /**
     * 批量删除金蝶-财务-应收票据结算单
     *
     * @param ids 需要删除的金蝶-财务-应收票据结算单ID
     * @return 结果
     */
    @Override
    public int deleteKingFinanceReceiptSettlementBillEntityByIds ( String[] ids ) {
        return kingFinanceReceiptSettlementBillEntityMapper.deleteKingFinanceReceiptSettlementBillEntityByIds ( ids );
    }

    /**
     * 删除金蝶-财务-应收票据结算单信息
     *
     * @param id 金蝶-财务-应收票据结算单ID
     * @return 结果
     */
    @Override
    public int deleteKingFinanceReceiptSettlementBillEntityById ( String id ) {
        return kingFinanceReceiptSettlementBillEntityMapper.deleteKingFinanceReceiptSettlementBillEntityById ( id );
    }

    /**
     * 根据往来单位类型，获取往来单位和付款单位
     *
     * @param companyType：单位类型，根据往来单位或付款单位的类型魔法值
     * @param companyId：单位Id，根据单位类型，选择的具体单位Id
     * @param type：标志位，根据其判断要查询名称还是编码，1：查询名称；2：查询编码
     * @return
     */
    private String getCompanyName ( String companyType, String companyId, Integer type ) {
        if ( StringUtils.isBlank ( companyType ) || StringUtils.isBlank ( companyId ) ) {
            return "";
        }
        String transactionCompany = "";
        switch ( companyType ) {
            case "BD_Customer":
                CustomerEntity customerEntity = iCustomerService.getById ( companyId );
                if ( customerEntity != null && type == 1 )
                    transactionCompany = customerEntity.getCustomerName ();
                else if ( customerEntity != null && type == 2 )
                    transactionCompany = customerEntity.getCustomerCode ();
                break;
            case "BD_Supplier":
                SupplierEntity supplierEntity = iSupplierService.getById ( companyId );
                if ( supplierEntity != null && type == 1 )
                    transactionCompany = supplierEntity.getSupplierName ();
                else if ( supplierEntity != null && type == 2 )
                    transactionCompany = supplierEntity.getSupplierCode ();
                break;
            case "BD_Department":
                SysDept sysDept = iSysDeptService.getById ( companyId );
                if ( sysDept != null && type == 1 )
                    transactionCompany = sysDept.getDeptName ();
                else if ( sysDept != null && type == 2 )
                    transactionCompany = sysDept.getDeptCode ();
                break;
            case "BD_Empinfo":
                SysUser sysUser = iSysUserService.getById ( companyId );
                if ( sysUser != null && type == 1 )
                    transactionCompany = sysUser.getNickName ();
                else if ( sysUser != null && type == 2 )
                    transactionCompany = sysUser.getUserCode ();
                break;
            case "FIN_OTHERS":
                KingBaseTransactionsCompanyEntity transactionsCompanyEntity = iKingBaseTransactionsCompanyService.getById ( companyId );
                if ( transactionsCompanyEntity != null && type == 1 )
                    transactionCompany = transactionsCompanyEntity.getTitle ();
                else if ( transactionsCompanyEntity != null && type == 2 )
                    transactionCompany = transactionsCompanyEntity.getCompanyCode ();
                break;
            case "ORG_Organizations":
                SysDept organ = iSysDeptService.getById ( companyId );
                if ( organ != null && type == 1 )
                    transactionCompany = organ.getDeptName ();
                else if ( organ != null && type == 2 )
                    transactionCompany = organ.getDeptCode ();
                break;
            case "BD_BANK":
                KingBaseBankEntity bankEntity = iKingBaseBankService.getById ( companyId );
                if ( bankEntity != null && type == 1 )
                    transactionCompany = bankEntity.getTitle ();
                else if ( bankEntity != null && type == 2 )
                    transactionCompany = bankEntity.getCode ();
                break;
        }
        return transactionCompany;
    }

}
